//
//  ViewController.m
//  01-UICollectionView流水布局
//
//  Created by 紫荆秋雪 on 16/3/4.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

#import "ViewController.h"

static NSString * const ID = @"cell";

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

/** 存储图片的数组 */
@property (nonatomic,strong) NSMutableArray *images;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建UICollectionView
    [self setupCollectionView];
    
}

- (void)setupCollectionView{
   
    //设置UICollectionView的frame
    CGRect frame = CGRectMake(40, 200, 300, 300);
    
    //1、创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    
    //2、注册cell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    
    //3、遵守代理
    collectionView.delegate = self;
    collectionView.dataSource = self;

    
    //添加到控制器view上面
    [self.view addSubview:collectionView];
}

#pragma mark - 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    
    return cell;
}

#pragma mark - 懒加载
- (NSMutableArray *)images
{
    if (_images == nil) {
        _images = [NSMutableArray array];
        
        //给数字添加图片
        for (int i = 0; i<18; i++) {
            
            //图片名
            NSString *imgName = [NSString stringWithFormat:@"%d.png", i];
            
            //把图片名存储到数组中
            [_images addObject:imgName];
        }
    }
    
    return _images;
}



@end
